package top.huahaizhi.mpchart.demo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.Random;

/**
 * xbo 折线图
 */
public class LineChartActivity2 extends AppCompatActivity {

    private LineChart lineChart;
    private Random random;
    private DrawThread drawThread;
    // 线段数量
    private int lineSize = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart2);
        random = new Random();
        lineChart = (LineChart) findViewById(R.id.lineChart);
        drawThread = new DrawThread();
        showData();
    }

    private void showData() {
        LineData lineData = getLineData();
        showChart(lineData);

        // 开启死循环
        drawThread.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                showData();
            }
        }, 3000);

    }

    private void showChart(LineData lineData) {
        lineChart.setDrawBorders(false); //在折线图上添加边框
        lineChart.setDescription(""); //数据描述
        lineChart.setNoDataTextDescription("You need to provide data for the chart.");

        lineChart.setDrawGridBackground(false); //表格颜色
        lineChart.setGridBackgroundColor(Color.BLACK); //表格的颜色，设置一个透明度

        lineChart.setTouchEnabled(true); //可点击

        lineChart.setDragEnabled(true);  //可拖拽
        lineChart.setScaleEnabled(true);  //可缩放

        lineChart.setPinchZoom(false);

        lineChart.setBackgroundColor(Color.WHITE); //设置背景颜色

        lineChart.setData(lineData);  //填充数据

        Legend mLegend = lineChart.getLegend(); //设置标示，就是那个一组y的value的

        mLegend.setForm(Legend.LegendForm.CIRCLE); //样式
        mLegend.setFormSize(6f); //字体
        mLegend.setTextColor(Color.BLACK); //颜色

        lineChart.setVisibleXRange(1, 7);   //x轴可显示的坐标范围
        XAxis xAxis = lineChart.getXAxis();  //x轴的标示
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); //x轴位置
        xAxis.setTextColor(Color.BLACK);    //字体的颜色
        xAxis.setTextSize(10f); //字体大小
        xAxis.setGridColor(getRandomColor());//网格线颜色
        xAxis.setDrawGridLines(false); //不显示网格线

        YAxis axisLeft = lineChart.getAxisLeft(); //y轴左边标示

        axisLeft.setTextColor(Color.BLACK); //字体颜色
        axisLeft.setTextSize(10f); //字体大小
        axisLeft.setAxisMaxValue(1000f); //最大值
        axisLeft.setLabelCount(6, true); //显示格数
        axisLeft.setGridColor(getRandomColor()); //网格线颜色

        YAxis axisRight = lineChart.getAxisRight(); //y轴右边标示
        axisRight.setDrawAxisLine(false);
        axisRight.setDrawGridLines(false);
        axisRight.setDrawLabels(false);

        lineChart.animateX(2500);  //立即执行动画
    }

    private LineData getLineData() {
        int count = 6;
        ArrayList<String> xValues = new ArrayList<String>();
        // x轴显示的数据
        for (int i = 0; i < count; i++) {
            // 这里默认使用数字下标显示
            xValues.add("" + (i + 1));
        }

        // 线段数据集
        ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();

        // 生成随机数据
        for (int i = 0; i < lineSize; i++) {
            ArrayList<Entry> yValues = new ArrayList<Entry>();
            // y轴的数据
            for (int j = 0; j < count; j++) {
                float value = (int) (Math.random() * 100);
                yValues.add(new Entry(value, j));
            }
            //用y轴的集合来设置参数
            int rgb = getRandomColor();
            // y轴的数据集合
            LineDataSet lineDataSet = new LineDataSet(yValues, "x" + (i + 1));
            // lineDataSet.setFillAlpha(110);
            // lineDataSet.setFillColor(Color.RED);
            lineDataSet.setLineWidth(1.75f); // 线宽
            lineDataSet.setDrawCircles(true);
            lineDataSet.setDrawCubic(true); // 绘制曲线
            lineDataSet.setDrawFilled(true); // 绘制覆盖面
            lineDataSet.setCircleSize(3f);// 显示的圆形大小
            lineDataSet.setColor(rgb);// 显示颜色
            lineDataSet.setCircleColor(rgb);// 圆形的颜色
            lineDataSet.setHighLightColor(rgb); // 高亮的线的颜色
            lineDataSet.setHighlightEnabled(true);
            lineDataSet.setValueTextColor(Color.BLACK); //数值显示的颜色
            lineDataSet.setValueTextSize(8f);     //数值显示的大小
            lineDataSet.setDrawVerticalHighlightIndicator(true);
            lineDataSet.setValueTypeface(Typeface.MONOSPACE);
            lineDataSets.add(lineDataSet); // 添加数据集合
        }
        return new LineData(xValues, lineDataSets);
    }

    /**
     * 生成随机颜色
     */
    public int getRandomColor() {
        return Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (drawThread != null) {
            drawThread.getHandler().removeCallbacksAndMessages(null);
            drawThread.getHandler().getLooper().quit();
            drawThread.setHandler(null);
            drawThread = null;
        }
    }
}